<!DOCTYPE html>
<html>
  <head>
    <title>Firebase + Validation</title>
    <script src="https://unpkg.com/vue"></script>
    <script src="https://unpkg.com/vuefire@1.3.0/dist/vuefire.js"></script>
    <script src="https://www.gstatic.com/firebasejs/3.4.0/firebase.js"></script>
    <link rel="stylesheet" type="text/css" href="/style.css" />
  </head>
  <body>
    <!-- Using vuefire (the official Firebase binding) -->
    <div id="app">
      <ul is="transition-group">
        <li v-for="user in users" class="user" :key="user['.key']">
          <span>{{user.name}} - {{user.email}}</span>
          <button v-on:click="removeUser(user)">X</button>
        </li>
      </ul>
      <form id="form" v-on:submit.prevent="addUser">
        <input type="text" v-model="newUser.name" placeholder="Username" />
        <input
          type="email"
          v-model="newUser.email"
          placeholder="email@email.com"
        />
        <input type="submit" value="Add User" />
      </form>
      <ul class="errors">
        <li v-show="!validation.name">Name cannot be empty.</li>
        <li v-show="!validation.email">
          Please provide a valid email address.
        </li>
      </ul>
    </div>

    <script>
      var emailRE = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

      // Setup Firebase
      var config = {
        apiKey: "AIzaSyAi_yuJciPXLFr_PYPeU3eTvtXf8jbJ8zw",
        authDomain: "vue-demo-537e6.firebaseapp.com",
        databaseURL: "https://vue-demo-537e6.firebaseio.com"
      };
      firebase.initializeApp(config);

      var usersRef = firebase.database().ref("users");

      // create Vue app
      var app = new Vue({
        // element to mount to
        el: "#app",
        // initial data
        data: {
          newUser: {
            name: "",
            email: ""
          }
        },
        // firebase binding
        // https://github.com/vuejs/vuefire
        firebase: {
          users: usersRef
        },
        // computed property for form validation state
        computed: {
          validation: function() {
            return {
              name: !!this.newUser.name.trim(),
              email: emailRE.test(this.newUser.email)
            };
          },
          isValid: function() {
            var validation = this.validation;
            return Object.keys(validation).every(function(key) {
              return validation[key];
            });
          }
        },
        // methods
        methods: {
          addUser: function() {
            if (this.isValid) {
              usersRef.push(this.newUser);
              this.newUser.name = "";
              this.newUser.email = "";
            }
          },
          removeUser: function(user) {
            usersRef.child(user[".key"]).remove();
          }
        }
      });
    </script>
  </body>
</html>
